import java.util.Arrays;

public class StateMachine {
    private State currentState = State.INITIAL;
    private StateHandler[] handlers;

    public StateMachine() {
        handlers = new StateHandler[]{
                new InitialStateHandler(this),       // INITIAL
                new InStringHandler(this),           // IN_STRING
                new EscapeCharHandler(this),         // ESCAPE_CHAR
                new StringEndHandler(this)           // STRING_END
        };
    }

    public void setState(State newState) {
        currentState = newState;
    }

    public void processString(String input) {
        StringBuilder currentString = new StringBuilder();
        for (char ch : input.toCharArray()) {
            handlers[currentState.ordinal()].handleChar(ch, currentString, this);
        }
    }

    public void printCurrentString(StringBuilder currentString) {
        System.out.println(currentString.toString());
    }

    public void resetCurrentString(StringBuilder currentString) {
        currentString.setLength(0); // 清空StringBuilder
    }
}